﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Brc_DSB.GetWay.Middleware;
using Ocelot;
using Ocelot.DependencyInjection;
using Ocelot.Provider.Consul;
using Ocelot.Provider.Polly;
using Brc_DSB.GetWay.Model;
using Brc_DSB.GetWay.Consul;
using Ocelot.Administration;
using IdentityServer4.AccessTokenValidation;
using Brc_DSB.GetWay.Polly;
using Exceptionless;

namespace Brc_DSB.Api
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            Action<IdentityServerAuthenticationOptions> options = o =>
            {
                o.ApiName = "brc_gateway_config_admin";
                o.Authority = "http://localhost:6612";
                o.RequireHttpsMetadata = false;
                o.SupportedTokens = SupportedTokens.Both;
            };
            //services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

            //services.AddAuthentication().AddIdentityServerAuthentication("brc_gateway", options);
            services.AddOcelot().AddConsul().AddPolly().AddBrcGetWay(option =>
            {
                option.DbConnectionStrings = "Server=.;Database=DSB;User ID=sa;Password=123456Aa;";
                option.RedisConfig = Configuration.GetSection("RedisConfig").Get<List<string>>();
                option.ClientAuthorization = true;
                option.IsEnableRetry = true;
                option.ExceptionlessKey = "Vv2dVVwYOfHR2Y6iDRjU4IIEpWlGHEfC7x9y0PMW";
                option.ExceptionlessAddress = "http://47.108.174.153:8445";

            }).AddAdministration("/admin", options);
           
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, IApplicationLifetime lifetime)
        {
            
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Error");
            }
            app.UseBrcGetWay(new GetWay.Configuration.DbConfiguration() {
                ExceptionlessKey = "Vv2dVVwYOfHR2Y6iDRjU4IIEpWlGHEfC7x9y0PMW" ,
                ExceptionlessAddress = "http://47.108.174.153:8445"}).Wait();            
            var consulEntity = new ConsulEntity()
            {
                ConsulIP = Configuration["Consul:IP"],
                ConsulPort = Convert.ToInt32(Configuration["Consul:Port"]),
            };
            var serviceList = Configuration.GetSection("Service").Get<List<ServiceEntity>>();
            app.RegisterConsul(lifetime, consulEntity, serviceList);

        }
    }
}
